In [1]:
(define in->pre
  (lambda (exp)
    (cond
     ((atom? exp) exp)
     (else (list (cadr exp)
                 (in->pre (car exp))
                 (in->pre (caddr exp)))))))
In [2]:
(in->pre '(1 + (2 * 6)))
Out[2]:
(+ 1 (* 2 6))
In [3]:
(define eval-infix
  (lambda (e)
    (eval (in->pre e))))
In [4]:
(eval-infix '(1 + 1))
Out[4]:
2
In [6]:
(define my-list?
  (lambda (thing)
    (cond
     ((null? thing) #t)
     ((atom? thing) #f)
     (else (my-list? (cdr thing))))))
In [7]:
(my-list? '())
Out[7]:
#t
In [8]:
(my-list? '(1))
Out[8]:
#t
In [9]:
(my-list? '(1 . 2))
Out[9]:
#f
In [10]:
(my-list? 1)
Out[10]:
#f
In [11]:
(my-list? '(1 2 3 4 5 6 . 9))
Out[11]:
#f
In [12]:
(define mylength
  (lambda (list)
    (cond
     ((null? list) 0)
     (else (+ 1 (mylength (cdr list)))))))
In [13]:
(mylength '())
Out[13]:
0
In [14]:
(mylength '(1))
Out[14]:
1
In [15]:
(mylength '(1 2 3))
Out[15]:
3
In [16]:
(define divide-and-leftover
  (lambda (m n)
    (d-and-l m n 0)))

(define d-and-l
  (lambda (m n count)
    (cond
     ((> n m) (list count m))
     (else (d-and-l (- m n) n (+ 1 count))))))
In [17]:
(divide-and-leftover 8 4)
Out[17]:
(2 0)
In [18]:
(divide-and-leftover 8 5)
Out[18]:
(1 3)
In [19]:
(divide-and-leftover 100 5)
Out[19]:
(20 0)
In [20]:
(divide-and-leftover 100 4)
Out[20]:
(25 0)
In [21]:
(divide-and-leftover 100 3)
Out[21]:
(33 1)
In [ ]:
;; Define a procedure set? that determines if it is
;; composed of unique atoms.

(define set?
  
In [23]:
(define my-sum
In [24]:
(my-sum '())
Out[24]:
0
In [25]:
(my-sum '(12))
Out[25]:
12
In [26]:
(my-sum '(12 23))
Out[26]:
35
In [ ]:
(define exactly-two?
  (lambda (a list)
In [ ]:
(exactly-two? 'a '())
In [ ]:
(exactly-two? 'a '(a a))
In [ ]:
(exactly-two? 'a '(a b c d))
In [ ]:
(exactly-two? 'a '(b c a d e a))
In [29]:
(define exactly-two-all?
  (lambda (list)
    (other list list)))
    
(define other
  (lambda (l1 l2)
    (cond
     ((null? l1) #t)
     ((= (count (car l1) l2) 2) (other (cdr l1) l2))
     (else #f))))
    
    
(define count
  (lambda (a list)
    (cond
     ((null? list) 0)
     ((eq? (car list) a) (+ 1 (count a (cdr list))))
     (else (count a (cdr list))))))
In [30]:
(exactly-two-all? '(a a b))
Out[30]:
#f
In [31]:
(exactly-two-all? '(b a a b))
Out[31]:
#t
In [34]:
(exactly-two-all? '(b a a b))
Out[34]:
#t
In [ ]:
;; Write a procedure that returns the number of
;; parens in a Scheme list.

(define count-parens
  (lambda (item)
    (cond)))
     
;; In: (count-parens 1)
;; Out: 0

;; In: (count-parens '())
;; Out: 2

;; In: (count-parens '((a) (b)))
;; Out: 6

;; Hint: it isn't really counting parens!